<template>
  <div :class="prefixCls">
    <quill-editor
      v-model="content"
      ref="myQuillEditor"
      :content="value"
      :options="editorOption"
      @blur="onEditorBlur($event)"
      @focus="onEditorFocus($event)"
      @ready="onEditorReady($event)"
      @change="onEditorChange($event)"
    >
    </quill-editor>
  </div>
</template>

<script>
import { quillEditor } from "vue-quill-editor";
import { addQuillTitle } from './quill-title.js'
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";

export default {
  name: "quill",
  components: { quillEditor },
  props: {
    prefixCls: {
      type: String,
      default: "jsbos-editor-quill",
    },
    // 表单校验用字段
    // eslint-disable-next-line
    value: {
      type: String,
    },
    placeholder: {
      type: String,
      default: "请输入内容...",
    },
    // 判断是否不能编辑
    notEditor: {
      type: Boolean,
      default: false,
    },
  },
  mounted(){
    addQuillTitle();
  },
  data() {
    return {
      // Q:{},
      content: this.value,
      editorOption: {
        modules: {
          toolbar: [
            ["bold", "italic", "underline", "strike"],
            [{ header: [1, 2, 3, 4, 5, 6, false] }],
            [{ size: ["small", false, "large", "huge"] }],
            [{ color: [] }, { background: [] }],
            ["blockquote", "code-block"],
            [{ list: "ordered" }, { list: "bullet" }],
            [{ indent: "-1" }, { indent: "+1" }],
            [{ align: [] }],
            [{ direction: "rtl" }],
            ["clean"],
            ["link", "image"],
          ],
        },
        theme: "snow",
        placeholder: this.placeholder,
      },
      copyQuill: null,
    };
  },
  created() {
    console.log(this.notEditor, "111111");
  },
  methods: {
    onEditorBlur(quill) {
      // console.log('editor blur!', quill)
    },
    onEditorFocus(quill) {
      // console.log('editor focus!', quill)
    },
    onEditorReady(quill) {
      this.copyQuill = quill;
      console.log(this.notEditor, quill, "0000000");
      // console.log("editor ready!", quill);
      // 禁用编辑器
      if (this.notEditor) {
        quill.enable(false);
      }
    },
    onEditorChange({ quill, html, text }) {
      // console.log('editor change!', quill, html, text)
      // this.$emit('change', html)
      this.$emit("input", this.content);
    },
  },
  computed: {
    editor() {
      return this.$refs.myQuillEditor.quill;
    },
  },
  watch: {
    notEditor(val) {
      this.copyQuill.enable(!val);
    },
    value(val) {
      this.content = val;
    },
    placeholder(val) {
      this.$set(this.editorOption, "placeholder", val);
    },
  },
};
</script>
<style>
.ql-editor {
  min-height: 300px;
}
</style>
<style lang="scss" scoped>
editor-quill {
  >>> .ql-editor {
    min-height: 300px;
  }
  >>> .ql-toolbar.ql-snow {
    border: 1px solid #dcdfe6;
    border-bottom: 0;
    border-radius: 4px 4px 0 0;
  }
  >>> .ql-container.ql-snow {
    border: 1px solid #dcdfe6;
    border-radius: 0 0 4px 4px;
  }
}
</style>
